package com.yanlin.servicedriveruser.controller;

import com.yanlin.internalcommon.dto.DriverUser;
import com.yanlin.internalcommon.dto.ResponseResult;
import com.yanlin.internalcommon.response.DriverUserExistsResponse;
import com.yanlin.servicedriveruser.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.yanlin.internalcommon.constant.DriverCarConstant.DRIVER_EXISTS;
import static com.yanlin.internalcommon.constant.DriverCarConstant.DRIVER_NOT_EXISTS;

/**
 * <p>标题：UserController</p>
 * <p>功能：</p>
 * <p>
 * 其他说明：
 * </p>
 * <p>审核：</p>
 * <p>重构：</p>
 * <p>创建日期：2025-06-12 21:38</p>
 *
 * @author yanlin
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public ResponseResult getUser(@RequestBody DriverUser user) {
        String driverPhone = user.getDriverPhone();
        log.info("司机手机：{}", driverPhone);
        return userService.getUser(driverPhone);
    }

    @PostMapping("/user")
    public ResponseResult addUser(@RequestBody DriverUser driverUser) {
        log.info("用户信息：{}", driverUser);
        return userService.addUser(driverUser);
    }

    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody DriverUser driverUser) {
        return userService.updateUser(driverUser);
    }

    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult getUser(@PathVariable String driverPhone) {
        log.debug("check-driver phone: {}", driverPhone);
        ResponseResult<DriverUser> result = userService.getUserByDriverPhone(driverPhone);
        DriverUser driverUserDb = result.getData();
        DriverUserExistsResponse responseView = new DriverUserExistsResponse();
        responseView.setDriverPhone(driverPhone);
        int ifExists = DRIVER_EXISTS;
        if (driverUserDb == null) {
            ifExists = DRIVER_NOT_EXISTS;
        }
        responseView.setIfExists(ifExists);
        return ResponseResult.success(responseView);
    }
}